package com.caipos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipos.actor.common.data.relation.Relation;
import com.caipos.mapper.RelationMapper;
import com.caipos.service.RelationService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelationServiceImpl extends ServiceImpl<RelationMapper, Relation> implements RelationService {
    @Override
    public Page<Relation> getRelationPage(Page<Relation> page) {
        return baseMapper.selectPage(page, null);
    }

    @Override
    public List<Relation> findRelationsByRelation(Relation relation) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        if (relation.getFromId() != null) {
            queryWrapper.eq("from_id", relation.getFromId());
        }
        if (relation.getFromType() != null) {
            queryWrapper.eq("from_type", relation.getFromType());
        }
        if (relation.getToId() != null) {
            queryWrapper.eq("to_id", relation.getToId());
        }
        if (relation.getToType() != null) {
            queryWrapper.eq("to_type", relation.getToType());
        }
        if (relation.getRelationTypeGroup() != null) {
            queryWrapper.eq("relation_type_group", relation.getRelationTypeGroup());
        }
        if (relation.getRelationType() != null) {
            queryWrapper.eq("relation_type", relation.getRelationType());
        }
        if (relation.getVersion() != null) {
            queryWrapper.eq("version", relation.getVersion());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean deleteByRelation(Relation relation) {
        QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
        if (relation.getFromId() != null) {
            queryWrapper.eq("from_id", relation.getFromId());
        }
        if (relation.getFromType() != null) {
            queryWrapper.eq("from_type", relation.getFromType());
        }
        if (relation.getToId() != null) {
            queryWrapper.eq("to_id", relation.getToId());
        }
        if (relation.getToType() != null) {
            queryWrapper.eq("to_type", relation.getToType());
        }
        if (relation.getRelationTypeGroup() != null) {
            queryWrapper.eq("relation_type_group", relation.getRelationTypeGroup());
        }
        if (relation.getRelationType() != null) {
            queryWrapper.eq("relation_type", relation.getRelationType());
        }
        if (relation.getVersion() != null) {
            queryWrapper.eq("version", relation.getVersion());
        }
        int rows = baseMapper.delete(queryWrapper);
        return rows > 0;
    }
}    